package sh.ds.el.exception;

/**
 * 业务异常
 * 
 * @author dongbz
 * @date 2012-9-25
 */
@SuppressWarnings("serial")
public class ServiceException extends Exception {

	public ServiceException(String errorMsg) {
		super(errorMsg);
	}
	
	public ServiceException(String errorMsg, Throwable cause) {
		super(errorMsg, cause);
	}
	
	@Override
	public synchronized Throwable fillInStackTrace() {
		return this;
	}
	
	public String getServiceMsg() {
		return getMessage();
	}
	
	@Override
    public String toString() {
		StringBuilder msg = new StringBuilder();
		msg.append(getClass().getName()).append(":").append(getServiceMsg()).append("\n");
		Throwable cause = null;
		for (int i = 1; (cause = super.getCause()) != null && cause != this; i ++) {
		    msg.append(getSpecifiedCountTab(i)).append(cause.getClass().getName()).append(":").append(cause.getMessage()).append("\n");
		}
		return msg.toString();
    }
	
	protected String getSpecifiedCountTab(int count) {
		if (count < 1) {
		    return null;
		}
		StringBuilder tab = new StringBuilder();
		for (int i = 0; i < count; i ++) {
		    tab.append("\t");
		}
		return tab.toString();
    }
	
}
